package com.demo.jianjunhuang.phonestatus;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button deviceIdBtn;
    private Button imeiBtn;
    private Button simiBtn;
    private Button macBtn;
    private Button brandBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
    }

    private void initListener() {
        deviceIdBtn.setOnClickListener(this);
        imeiBtn.setOnClickListener(this);
        simiBtn.setOnClickListener(this);
        macBtn.setOnClickListener(this);
        brandBtn.setOnClickListener(this);
    }

    private void initView() {
        deviceIdBtn = getView(R.id.device_id_btn);
        imeiBtn = getView(R.id.iemi_id_btn);
        simiBtn = getView(R.id.sim_number_btn);
        macBtn = getView(R.id.mac_id_btn);
        brandBtn = getView(R.id.brand_id_btn);
    }

    private <T extends View> T getView(int id) {
        View view = findViewById(id);
        return (T) view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.device_id_btn: {
                String deviceId =
                        Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
                deviceIdBtn.setText(deviceId);
                break;
            }
            case R.id.iemi_id_btn: {
                getPermission(Manifest.permission.READ_PHONE_STATE, 0);
                break;
            }
            case R.id.sim_number_btn: {
                getPermission(Manifest.permission.READ_PHONE_STATE, 1);
                break;
            }
            case R.id.mac_id_btn: {
                macBtn.setText(getMacAddr());
                break;
            }
            case R.id.brand_id_btn: {
                brandBtn.setText(Build.BRAND + " " + Build.BOARD + " " + Build.DEVICE + " " + Build.MODEL);
                break;
            }
        }
    }


    public static String getMacAddr() {
        try {
            List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface nif : all) {
                if (!nif.getName().equalsIgnoreCase("wlan0")) continue;

                byte[] macBytes = nif.getHardwareAddress();
                if (macBytes == null) {
                    return "";
                }

                StringBuilder res1 = new StringBuilder();
                for (byte b : macBytes) {
                    res1.append(String.format("%02X:", b));
                }

                if (res1.length() > 0) {
                    res1.deleteCharAt(res1.length() - 1);
                }
                return res1.toString();
            }
        } catch (Exception ex) {
        }
        return "02:00:00:00:00:00";
    }

    private void getPermission(String permission, int resultCode) {
        if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                //show
                Toast.makeText(this, "we need this permission", Toast.LENGTH_SHORT).show();
                ActivityCompat.requestPermissions(this, new String[]{permission}, resultCode);
            } else {
                ActivityCompat.requestPermissions(this, new String[]{permission}, resultCode);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 0) {
            TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            String imei = telephonyManager.getDeviceId();
            imeiBtn.setText(imei);
        } else if (requestCode == 1) {
            TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            String sim = telephonyManager.getSubscriberId();
            simiBtn.setText(sim);
        }
    }
}
